// sd_liferuning/pages/service/userauth-pay/index.js
var api = require("../../../api.js"),app=getApp();
var WxParse = require('../../../wxParse/wxParse.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_data:0,
    current_money_index: 0,
    current_money:"",
    current_id:"",
    runner: { bond_price: "0.00", bond_id: 1 },
    getMoney: [
      { id: 1,
       promise_money: "0.01", 
       des: "<p>1232</p >",
        money:"20000.00", 
        num: 100, 
        name: "跑腿默认保证金1"
      }, {
        id: 2,
        promise_money: "0.01",
        des: "<p>1232</p >",
        money: "20000.00",
        num: 100,
        name: "跑腿默认保证金1"
      }, {
        id: 3,
        promise_money: "0.01",
        des: "<p>1232</p >",
        money: "20000.00",
        num: 100,
        name: "跑腿默认保证金1"
      }, {
        id: 4,
        promise_money: "0.01",
        des: "<p>1232</p >",
        money: "20000.00",
        num: 100,
        name: "跑腿默认保证金1"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that=this;
    
   app.request({
     url: api.promise_money.index,
      method: 'post',
      data:{
        // uid:wx.getStorageSync('uid')
      },
      success:function(e){
        if (e.code == 1) {//查询保证金金额
          var data = e.data;
          that.setData({ 
            getMoney: e.data.data,
            runner: e.data.promise_money,
            bind: e.data.hand,
          });
          WxParse.wxParse('article', 'html', e.data.des, that, 5)
        } else {
        }
      }
   });
  },
  pay:function() {
    const { runner}=this.data;
    app.request({
      url: api.driver.margin,
      data:{
        amount: runner
      },
      method: 'post',
      success:function(res){
         if(res.code==1){
            wx.requestPayment({
              timeStamp:res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: 'MD5',
              paySign: res.data.paySign,
              success:function(ress){
                console.log("current_id",ress);
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              },
              fail: function () {
               
              }
            })
          }else if(res.code===0){
            wx.showToast({
              title: res.msg,
              icon:'none'
            })
          }
          else{
            wx.showModal({
              title: '提示',
              content: '支付失败',
            })
          }
        }
    })
  },
  /**保证金退款 */
  outpay:function(){
    let that=this;
    wx.showModal({
      title: '提示',
      content: '您确定要退还押金？',
      success:function(e){
        if(e.confirm){
          wx.showModal({
            title: '提示',
            content: '退款需要人工审核，退还到余额中',
            success:function(res){
              app.request({
                url: api.driver.refund,
                method: 'post',
                data: {
                  is_margin:3,
                  amount: that.data.runner
                },
                success: function (e) {
                  console.log(e)
                  if(e.code == 1){
                    wx.showToast({
                      title: e.msg,
                      icon: 'none'
                    })
                    setTimeout(function(){
                      wx.reLaunch({
                        url: '/pages/index/index',
                      })
                    },1500)
                  }else{
                    wx.showToast({
                      title: e.msg,
                      icon:'none'
                    })
                  }
                }
              })
            }
          })
        }else{
        }
      }
    })
  },
  // 选取保证金
  selectGetMoney:function(res){
    let that = this;
    console.log("this is money id", res.currentTarget.dataset.id);
    that.setData({ 
      current_money: that.data.getMoney[res.currentTarget.dataset.selected], 
      current_money_index: res.currentTarget.dataset.selected, 
      current_id: res.currentTarget.dataset.id
    })
    WxParse.wxParse('article', 'html', that.data.getMoney[res.currentTarget.dataset.selected].des, that, 5)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    
    
  },

})